package com.hdy.javachatroomdemo.Service;

import com.hdy.javachatroomdemo.Model.Message;
import com.hdy.javachatroomdemo.Model.MessageMapper;
import com.hdy.javachatroomdemo.Model.ShowMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class MessageService {
    @Autowired
    private MessageMapper messageMapper;
    public String queryLastMassage(int sessionId){
        Message message=messageMapper.queryLastMessage(sessionId);
        //此处查询消息结果可能没有数据要判断一下
        if(message!=null){
            return message.getContent();
        }else{
            return "";
        }
    }
    public List<ShowMessage> queryAllShowMessage(int sessionId){
        List<ShowMessage> ret= messageMapper.queryAllShowMessage(sessionId);
        //可能没有历史消息,判断一下
        if(ret!=null){
            Collections.reverse(ret);
            return ret;
        }else{
            System.out.println("[MessageService:queryAllShowMessage]sessionId:"+sessionId+" :未查询到任何历史消息");
            return new ArrayList<ShowMessage>();
        }
    }
}
